﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;
using NetCMS.Content.Common;
using NetCMS.Publish;

public partial class manage_channel_publish : NetCMS.Web.UI.ManagePage
{
    public manage_channel_publish()
    {
        this.Authority_Code = "D012";
    }
    Channel rd = new Channel();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SiteID != "0")
            {
                PageError("分站没权限操作", "javascript:history.back();", true);
            }
            copyright.InnerHtml = CopyRight;
            string ChID = Request.QueryString["ChID"];
            if (ChID == string.Empty || ChID == null)
            {
                PageError("错误的参数", "javascript:history.back()", true);
            }
            else
            {
                GetClassList((this.divClassNews), 0, 0);
                GetClassList((this.divClassClass), 0, 0);
                GetSpecialList((this.DivSpecial), 0, 0);
            }
        }
    }

    protected void GetClassList(ListBox lst, int ParentID,int Layer)
    {
        IDataReader dr = rd.getClassList(ParentID, int.Parse(Request.QueryString["ChID"]));
        while (dr.Read())
        {
            ListItem it = new ListItem();
            string stxt = "";
            it.Value = dr["ID"].ToString();
            if (Layer > 0)
                stxt = "┝";
            for (int i = 1; i < Layer; i++)
            {
                stxt += " ┉ ";
            }
            it.Text = stxt + dr["ClassCName"].ToString();
            lst.Items.Add(it);
            GetClassList(lst, int.Parse(dr["ID"].ToString()), (Layer + 1));
        }
        dr.Close();
    }

    protected void GetSpecialList(ListBox lst, int ParentID, int Layer)
    {
        IDataReader dr = rd.getSpecialList(ParentID, int.Parse(Request.QueryString["ChID"]));
        while (dr.Read())
        {
            ListItem it = new ListItem();
            string stxt = "";
            it.Value = dr["ID"].ToString();
            if (Layer > 0)
                stxt = "┝";
            for (int i = 1; i < Layer; i++)
            {
                stxt += " ┉ ";
            }
            it.Text = stxt + dr["specialCName"].ToString();
            lst.Items.Add(it);
            GetSpecialList(lst, int.Parse(dr["ID"].ToString()), (Layer + 1));
        }
        dr.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.CheckAdminAuthority();
        if (Page.IsValid)
        {
            showweb.Visible = false;
            
            bool indexBoolFlag = false;
            //读取发布主页的参数
            if (indexTF.Checked)
            {
                indexBoolFlag = true;
            }
            int newsFlag = 0;//读取发布新闻的参数
            bool newsBoolFlag = false;
            string strNewsParams = null;
            if (newsall.Checked)
            {
                newsFlag = 0;
                newsBoolFlag = true;
            }
            else
            {
                if (newslast.Checked)
                {
                    newsFlag = 1;
                    strNewsParams = NewNum.Text;
                    if (NetCMS.Common.Input.IsInteger(strNewsParams) == false)
                    {
                        PageError("发布最新的新闻请填写正确数字", "javascript:hitory.back();", true);
                    }
                    newsBoolFlag = true;
                }
                else
                {
                    if (newsunhtml.Checked)
                    {
                        newsFlag = 2;
                        strNewsParams = unhtmlNum.Text;
                        if (NetCMS.Common.Input.IsInteger(strNewsParams) == false)
                        {
                            PageError("发布未生成的新闻请填写正确数字", "javascript:hitory.back();", true);
                        }
                        newsBoolFlag = true;
                    }
                    else
                    {
                        if (newsclass.Checked)
                        {
                            newsFlag = 3;
                            strNewsParams = getNewsClassParams() + "$";
                            if (unHTMLnews.Checked)
                            {
                                strNewsParams += "#";//存在则只发布未发布的
                            }
                            newsBoolFlag = true;
                        }
                        else
                        {
                            if (newsdate.Checked)
                            {
                                newsFlag = 4;
                                if (NetCMS.Common.Input.IsDate(startTime.Text) == false && NetCMS.Common.Input.IsDate(endTime.Text) == false)
                                {
                                    PageError("请正确填写开始时间和结束时间", "javascript:hitory.back();", true);
                                }
                                strNewsParams = (startTime.Text).ToString() + "$" + (endTime.Text).ToString();
                                newsBoolFlag = true;
                            }
                            else
                            {
                                if (newsid.Checked)
                                {
                                    newsFlag = 5;
                                    if ((NetCMS.Common.Input.IsInteger(startID.Text) == false) || (NetCMS.Common.Input.IsInteger(EndID.Text) == false))
                                    {
                                        PageError("请正确ID开始及ID结束", "javascript:hitory.back();",true);
                                    }
                                    strNewsParams = startID.Text + "$" + EndID.Text;
                                    newsBoolFlag = true;
                                }
                            }
                        }
                    }
                }
            }

            int newsClassFlag = 1;//取发布栏目的参数
            string strNewsClassParams = string.Empty;
            bool classBoolFlag = false;
            if (classall.Checked)
            {
                newsClassFlag = 0;
                if (unHTMLclass.Checked)
                {
                    strNewsClassParams = "#";
                }
                classBoolFlag = true;
            }
            else
            {
                strNewsClassParams = getClassParams();
                if (strNewsClassParams != null)
                {
                    classBoolFlag = true;
                }
            }

            int specialFlag = 1;
            string strSpecialParams = string.Empty;
            bool specialBoolFlag = false;//取发布专题的参数
            if (specialall.Checked)
            {
                specialFlag = 0;
                specialBoolFlag = true;
            }
            else
            {
                strSpecialParams = getSpecialParams();
                if (strSpecialParams != null)
                {
                    specialBoolFlag = true;
                }
            }

            UltiPublish publishAll = new UltiPublish(true);
            publishAll.IsCHPublishIndex = indexBoolFlag;
            publishAll.IsCHPubNews = newsBoolFlag;
            publishAll.IsCHPubClass = classBoolFlag;
            publishAll.IsCHPubSpecial = specialBoolFlag;

            publishAll.CHnewsFlag = newsFlag;
            publishAll.strNewsCHParams = strNewsParams;
            publishAll.ClassCHFlag = newsClassFlag;
            publishAll.strClassCHParams = strNewsClassParams;
            publishAll.CHspecialFlag = specialFlag;
            publishAll.strCHSpecialParams = strSpecialParams;
            int ChID = int.Parse(Request.QueryString["ChID"]);
            publishAll.intCHID = ChID;
            publishAll.strCHTalbe = rd.getChannelTable(ChID);
            try
            {
                publishAll.StartCHPublish();
            }
            finally
            {
                publishAll.CloseAllConnection();
            }
            Response.End();
        }
    }

    /// <summary>
    /// 获取发布栏目时的参数字符串
    /// </summary>
    /// <returns>发布栏目时的参数字符串</returns>
    public string getClassParams()
    {
        string classIds = null;
        for (int i = 0; i < divClassClass.Items.Count; i++)
        {
            if (divClassClass.Items[i].Selected)
            {
                if (classIds != null)
                {
                    classIds += "$" + divClassClass.Items[i].Value;
                }
                else
                {
                    classIds = divClassClass.Items[i].Value;
                }
            }
        }
        return classIds;
    }

    /// <summary>
    /// 获取选中newsClass时的参数字符串
    /// </summary>
    /// <returns>选中newsClass时的参数字符串</returns>
    public string getNewsClassParams()
    {
        string classIds = null;
        for (int i = 0; i < divClassNews.Items.Count; i++)
        {
            if (divClassNews.Items[i].Selected)
            {
                if (classIds != null)
                {
                    classIds += "$" + divClassNews.Items[i].Value;
                }
                else
                {
                    classIds = divClassNews.Items[i].Value;
                }
            }
        }
        return classIds;
    }

    /// <summary>
    /// 获取发布专题时的参数字符串
    /// </summary>
    /// <returns>发布专题时的参数字符串</returns>
    public string getSpecialParams()
    {
        string classIds = null;
        for (int i = 0; i < DivSpecial.Items.Count; i++)
        {
            if (DivSpecial.Items[i].Selected)
            {
                if (classIds != null)
                {
                    classIds += "$" + DivSpecial.Items[i].Value;
                }
                else
                {
                    classIds = DivSpecial.Items[i].Value;
                }
            }
        }
        return classIds;
    }

}
